package cn.huanghuai.reggie.controller;


import cn.huanghuai.reggie.common.BaseContext;
import cn.huanghuai.reggie.common.R;
import cn.huanghuai.reggie.entity.ShoppingCart;
import cn.huanghuai.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.org.apache.bcel.internal.generic.LMUL;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id，指定当前是哪个用户的购物车数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //查询添加的是菜品还是套餐
            //创建条件过滤器,以及共用条件
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            //添加是菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加是套餐
            lambdaQueryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }
        //执行查询
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);

        //查询当前菜品或者套餐是否在购物车中
        if (one != null){
            //如何存在，则在原来数量上加一
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        }else {
            //不存在，则添加购物车，数量默认为1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }

        return R.success(one);
    }

    /**
     * 根据userId查询购物车商品内容
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> shoppingCartList(){
        Long currentId = BaseContext.getCurrentId();

        //添加条件过滤器
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加过滤条件
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
            //添加排序条件
        lambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
            //执行查询
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);

        return R.success(shoppingCartList);
    }


    /**
     * 清空购物车功能
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> shoppingCartClean(){
        Long currentId = BaseContext.getCurrentId();

        //添加条件过滤器
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加过滤条件
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
            //执行删除
        shoppingCartService.remove(lambdaQueryWrapper);

        return R.success("清空购物车成功");
    }

}
